package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.situ.web.dao.IBanjiDao;
import com.situ.web.entity.Banji;
import com.situ.web.util.JDBCUtil;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.sun.org.apache.regexp.internal.recompile;

public class IBanjiDaoimpl implements IBanjiDao {
	@Override
	public List<Banji> selectAll() {
		ArrayList<Banji> list = new ArrayList<Banji>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
		String sql = "SELECT id,`name` FROM banji";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");		
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
		for (Banji banji : list) {
			System.out.println(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
	  return list;
	}
	@Override
	public List<Banji> selectByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer insert(Banji banji) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(Banji banji) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from banji where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			System.out.println(preparedStatement);
			 count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);

		}
		return count;
	}

}
